local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local timetool = require "timetool"
local tabletool = require"tabletool"
require "gang28enum"
local tablelogic 	-- = require "tablelogic"
local seatlogic 	-- = require "seatlogic"
local gamelogic 	-- = require "gamelogic"

local server, table_data
local TableRequest = {}

function TableRequest.init(base)
    server = base
    table_data = server.table_data
	tablelogic = server.logic.tablelogic
	seatlogic = server.logic.seatlogic
	gamelogic = server.logic.gamelogic
end

function TableRequest.disconnect(rid, rdata, request)
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		return false
	end
	tablelogic.disconnect(table_data, request, seat)
    return true
end

function TableRequest.entertable(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	if server.will_kill then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "服务器维护中"
		return responsemsg
	end

	if table_data.conf.match_type and table_data.delete_table_timer_id and table_data.delete_table_timer_id > 0 then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "桌子已经刪除！"
		return responsemsg
	end

	if msghelper:callmsg_to_gatesvr("check_role_state", rid, 0) then
		responsemsg.retcode = EErrCode.ERR_HAD_IN_TABLE
		responsemsg.retstr = "已经在游戏中了！"
		filelog.error("check_role_state: 已经在游戏中了！")
		return responsemsg
	end

	--判断玩家金币是否够最小携带
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_DIAMOND then
		if not rdata.money.diamond or rdata.money.diamond < table_data.conf.min_carry_diamond then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
			responsemsg.retstr = "您的元宝不足，请前往充值！"
			return responsemsg
		end
	elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
		--判断玩家金币是否够最小携带
		-- if not rdata.money.coin or rdata.money.coin < table_data.conf.min_carry_diamond then
		if not rdata.money.coin or (rdata.money.coin < 10) or (rdata.money.coin + rdata.money.diamond < table_data.conf.min_carry_diamond) then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_COIN
			responsemsg.retstr = "您的友豆不足，请前往充值！"
			return responsemsg
		end
	end
	
	if tablelogic.is_full(table_data) then
		responsemsg.retcode = EErrCode.ERR_TABLE_FULL
		responsemsg.retstr = "房间人数已满！"
		return responsemsg
	end

	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat then
		seat.playerinfo.rolename = rdata.info.rolename
		seat.playerinfo.logo = rdata.info.logo
		seat.playerinfo.sex = rdata.info.sex
	end

	responsemsg.gameinfo = {}
	msghelper:copy_table_gameinfo(responsemsg.gameinfo)

	tablelogic.entertable(table_data, rid, rdata, request, seat)
	
	-- if table_data.conf.room_type == ERoomType.ROOM_TYPE_SYSTEM then
	-- 	if rid < 100 then
	-- 		TableRequest.sitdowntable(rid, rdata, request)
	-- 	end
	-- end
    return responsemsg
end

function TableRequest.reentertable(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, rid)
    local waitinfo = table_data.waits[rid]
	if seat ~= nil then
		seat.playerinfo.rolename = rdata.info.rolename
		seat.playerinfo.logo = rdata.info.logo
		seat.playerinfo.sex = rdata.info.sex
		seat.playerinfo.fangka = rdata.money.fangka
		seat.getfangka = rdata.money.fangka
		if table_data.conf.room_type == ERoomType.ROOM_TYPE_DIAMOND then
			if seat.cdiamond ~= rdata.money.diamond then
				local diff = seat.cdiamond - rdata.money.diamond
				seat.diamond = seat.diamond - diff
				seat.cdiamond = rdata.money.diamond
				seat.playerinfo.diamond = rdata.money.diamond
				seat.playerinfo.coin = rdata.money.coin
			end
		elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
			if seat.cdiamond ~= rdata.money.coin then
				local diff = seat.cdiamond - rdata.money.coin
				seat.diamond = seat.diamond - diff
				seat.cdiamond = rdata.money.coin
				seat.playerinfo.diamond = rdata.money.diamond
				seat.playerinfo.coin = rdata.money.coin
			end
		end
		if seat.is_tuoguan == EBOOL.TRUE then
			seat.is_tuoguan = EBOOL.FALSE
			seat.timeout_fold = 0
			seat.is_disconnect = nil
		end
	elseif waitinfo ~= nil then
		waitinfo.playerinfo.rolename = rdata.info.rolename
		waitinfo.playerinfo.logo = rdata.info.logo
		waitinfo.playerinfo.sex = rdata.info.sex		
	end

	if waitinfo == nil and seat == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效的请求！"
		return responsemsg
	end

	responsemsg.gameinfo = {}
	msghelper:copy_table_gameinfo(responsemsg.gameinfo)
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_FRIEND then
		local isallready = true
		for k, v in ipairs(table_data.seats) do
			if v.state ~= ESeatState.SEAT_STATE_WAIT_START then
				isallready = false
				break
			end
		end
		-- if responsemsg.gameinfo.state == ETableState.TABLE_STATE_WAIT_ALL_READY then
		-- 	if table_data.cur_games_times > 1 and table_data.cur_games_times <= table_data.conf.game_times and not isallready then
		-- 		responsemsg.gameinfo.state = ETableState.TABLE_STATE_WAIT_NEXT_GAMES
		-- 	end
		-- end
	end
	responsemsg.handcards = nil

	if not tablelogic.is_onegameend(table_data) and seat ~= nil then
		responsemsg.handcards = tabletool.deepcopy(seat.cards)
	end
	
	responsemsg.sametabledata = {}
	if table_data.conf.match_type then
		for k, v in ipairs(table_data.seats) do
			if v.sametabledata then
				table.insert(responsemsg.sametabledata, {
					rid = v.rid,
					countdata = v.sametabledata,
				})
			end
		end
	end

	if seat then
		seat.waitreenter = nil
		msghelper:sendmsg_to_tableplayer(seat, "reentertableR", responsemsg)
		tablelogic.reentertable(table_data, request, seat, rdata)	 
	end
	-- return responsemsg
end

function TableRequest.leavetable(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	request.rid = rid
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_DIAMOND
		or table_data.conf.room_type == ERoomType.ROOM_TYPE_FRIEND  --朋友桌房卡场没有退出
		or table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
		if seat == nil then
			tablelogic.leavetable(table_data, request, seat)
			return responsemsg
		end
		if seat.state ~= ESeatState.SEAT_STATE_WATCHING then
			if not tablelogic.is_onegameend(table_data) then
				responsemsg.retcode = EErrCode.ERR_GAMING
				responsemsg.retstr = "游戏中，不能退出!"
				return responsemsg
			end
		end
	end
	tablelogic.standuptable(table_data, request, seat)
	tablelogic.leavetable(table_data, request, seat)	
	return responsemsg
end

function TableRequest.sitdowntable(rid, rdata, request)
 	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	request.rid = rid
	if seat ~= nil then
		seat.playerinfo.rolename = rdata.info.rolename
		seat.playerinfo.logo = rdata.info.logo
		seat.playerinfo.sex = rdata.info.sex
		return responsemsg
	else
		if tablelogic.is_full(table_data) then
			if table_data.waits[rid] then
				tablelogic.leavetable(table_data, request)
			end

			responsemsg.retcode = EErrCode.ERR_TABLE_FULL
			responsemsg.retstr = "当前桌子已经满了！"
			return responsemsg
		end

		seat = tablelogic.get_emptyseat_by_index(table_data)
		if seat == nil then
			if table_data.waits[request.rid] then
				tablelogic.leavetable(table_data, request)
			end
			responsemsg.retcode = EErrCode.ERR_NO_EMPTY_SEAT
			responsemsg.retstr = "当前桌子没有空座位了！"
			return responsemsg	
		end
	end
	tablelogic.sitdowntable(table_data, rid, rdata, request, seat)

	-- if table_data.conf.room_type == ERoomType.ROOM_TYPE_SYSTEM then
	-- 	if request.rid < 100 then
	-- 		TableRequest.gameready(request)
	-- 	end
	-- end
    return responsemsg
end

function TableRequest.standuptable(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat then
		if seat.state == ESeatState.SEAT_STATE_PLAYING then
			responsemsg.retcode = EErrCode.ERR_GAMING
			responsemsg.retstr = "游戏中，不能退出"
			return responsemsg
		end
	else
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "你已经站起了！"
		return responsemsg
	end
	tablelogic.standuptable(table_data, request, seat)
	return responsemsg
end

function TableRequest.gameready(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	if server.will_kill then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "服务器即将维护，请退出房间！"
		return responsemsg
	end

	if table_data.state ~= ETableState.TABLE_STATE_WAIT_ALL_READY then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！不是等待准备状态"
		-- filelog.sys_error("TableRequest.gameready: 无效请求！不是等待准备状态")
		return responsemsg	
	end

	-- if table_data.cur_games_times > table_data.conf.game_times then
	-- 	responsemsg.retcode = EErrCode.ERR_GAME_TIMES_OVER
	-- 	responsemsg.retstr = "牌桌局数已经打完！"
	-- 	filelog.sys_error("TableRequest.gameready: 牌桌局数已经打完！")
	-- 	return responsemsg
	-- end

	request.rid = rid
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		filelog.sys_error("TableRequest.gameready: 玩家不在座位上！")
		return responsemsg
	end
	if seat.state ~= ESeatState.SEAT_STATE_WAIT_READY then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "座位状态不是等待准备状态！"
		filelog.sys_error("TableRequest.gameready: 座位状态不是等待准备状态！")
		return responsemsg
	end

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_DIAMOND then
		if seat.diamond < table_data.conf.leave_carry_diamond then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
			responsemsg.retstr = "您的元宝不足，请前往充值！"
			filelog.sys_error("TableRequest.gameready: 砖石不够了，先充值吧！", seat.diamond, table_data.conf.leave_carry_diamond)
			return responsemsg
		end
	elseif table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
		if seat.diamond < 10 or (seat.diamond + seat.playerinfo.diamond < table_data.conf.leave_carry_diamond) then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
			responsemsg.retstr = "您的元宝不足，请前往充值！"
			filelog.sys_error("TableRequest.gameready: 砖石不够了，先充值吧！", seat.diamond, table_data.conf.leave_carry_diamond)
			return responsemsg
		end
	end

	responsemsg.state = ESeatState.SEAT_STATE_WAIT_START
	tablelogic.gameready(table_data, request, seat)
	return responsemsg
end

function TableRequest.startgame(rid, rdata, request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	if table_data.state ~= ETableState.TABLE_STATE_WAIT_ALL_READY then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！"
		return responsemsg
	end
	
	if not tablelogic.startgame(table_data, request) then
		responsemsg.retcode = EErrCode.ERR_PLAYER_NOTENOUGH
		responsemsg.retstr = "最小人数不足"
		return responsemsg
	end
	return responsemsg
end

-- 抢庄
function TableRequest.call(rid, rdata, request)
	request.value = request.value or 0
	local responsemsg = {}
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		return responsemsg
	end
	if table_data.state ~= ETableState.TABLE_STATE_CALL then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！不是等待抢庄状态!"
		return responsemsg
	end
	if request.value == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "无效请求！参数为空！"
		return responsemsg
	end
	if request.value > 0 and not table_data.bankercallers[request.value] then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！参数不合法！"
		return responsemsg
	end
	if seat.call > 0 then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！已经抢过庄了!"
		return responsemsg
	end
	if request.value < 0 then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！参数错误！"
		return responsemsg
	end
	tablelogic.call(table_data, seat, request.value)
	return responsemsg
end

-- 下注
function TableRequest.bets(rid, rdata, request)
	request.value = request.value or 1
	local responsemsg = {}
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		return responsemsg
	end
	if table_data.state ~= ETableState.TABLE_STATE_BET then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！不是等待下注状态!"
		return responsemsg
	end
	if request.value == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_PARAMS
		responsemsg.retstr = "无效请求！参数错误！"
		return responsemsg
	end
	if seat.bets > 0 then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！已经下过注了!"
		return responsemsg
	end
	if seat.index == table_data.banker then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！庄家不能下注!"
		return responsemsg
	end

	if request.value < 0 then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！参数错误！"
		return responsemsg
	end
	if seat.diamond < request.value then
		responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
		responsemsg.retstr = "您的元宝不足，请前往充值！"
		return responsemsg
	end
	tablelogic.bets(table_data, seat, request.value)
	return responsemsg
end

-- 开牌
function TableRequest.showcards(rid, rdata, request)
	local responsemsg = {}
	local seat = tablelogic.get_seat_by_rid(table_data, rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		return responsemsg
	end
	if seat.showcard then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！已经开过牌了!"
		return responsemsg
	end
	if not seat.cards or #seat.cards ~= 2 then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！不能开牌!"
		return responsemsg
	end
	tablelogic.showcards(table_data, seat)
	return responsemsg
end

--[=[
--[[
//获取彩池相关信息 
message getjackoptinfo {
	
}

message getjackoptinfoR {
	optional int32 jackopt_num = 1; // 彩池总金额
	optional int32 money_type = 2;
	repeated getjackopt getjackopts = 3; // 瓜分比例和牌型
	repeated getjackoptrecord getjackoptrecords = 4; // 奖池瓜分详情
}
--]]
---玩家点击彩池图标获取彩池信息
function TableRequest.getjackoptinfo(request)
	local responsemsg = {}

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_COIN then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "金币场没有彩池"
		return responsemsg
	end

	local status, result = skynet.pcall(skynet.call, ".jackpotmgr", "lua", "getjackInfo", table_data.conf.base_score)
	if not status or next(result) == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "获取彩池信息失败"
		return responsemsg
	end
	
    return result
end
-]=]

return TableRequest